package com.example.administrator.siku.fragments;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.siku.R;
import com.example.administrator.siku.menu.beans.MenuBean;
import com.example.administrator.siku.menu.beans.TypeBean6;
import com.example.administrator.siku.menu.beans.TypeBean6_Bean;
import com.example.administrator.siku.menu.beans.UrlContent;
import com.example.administrator.siku.menu.beans.adapters.RecycleViewAdapter;
import com.example.administrator.siku.menu.beans.widget.PullToRefreshRecycleView;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class MenuFragment extends Fragment {
    private Toolbar toolbar;
    private DrawerLayout drawerLayout;
    private PullToRefreshRecycleView recycleView;
    private RecycleViewAdapter recycleViewAdapter;
    private Gson gson = new Gson();
    private List<MenuBean.FloorsBean> list = new ArrayList<>();
    private List<TypeBean6_Bean.RushRobListBean> list6 = new ArrayList<>();

    public MenuFragment() {
        // Required empty public constructor
    }
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            getList6();
        }
    };



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_menu, container, false);
        toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        drawerLayout = (DrawerLayout) view.findViewById(R.id.dl_content);
        recycleView = (PullToRefreshRecycleView) view.findViewById(R.id.ptrrv);
        recycleViewAdapter = new RecycleViewAdapter(getActivity(),list,gson,list6);
        RecyclerView refreshableView = recycleView.getRefreshableView();
        refreshableView.setAdapter(recycleViewAdapter);
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        refreshableView.setLayoutManager(manager);
        initView();
        initData();
        return view;
    }

    private void getList6() {
        String url = list.get(5).getContent();
        TypeBean6 typeBean6 = gson.fromJson(url, TypeBean6.class);
        final Request request1 = new Request.Builder().url(typeBean6.getUrl()).build();
        OkHttpClient okhttp1 = new OkHttpClient();
        Call call1 = okhttp1.newCall(request1);
        call1.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String s = response.body().string();
                TypeBean6_Bean typeBean6_bean = gson.fromJson(s, TypeBean6_Bean.class);
                if (typeBean6_bean!=null) {
                    list6.addAll(typeBean6_bean.getRush_rob_list());
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Log.e("run: ",list6.toString());
                            reflashData();
                        }
                    });
                }
            }
        });
    }

    private void reflashData() {
        recycleViewAdapter.notifyDataSetChanged();
    }

    private void initData() {
        Request request = new Request.Builder().url(UrlContent.getMenuUrl()).build();
        OkHttpClient okhttp = new OkHttpClient();
        Call call = okhttp.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                MenuBean menuBean = gson.fromJson(response.body().string(), MenuBean.class);
                if (menuBean!=null) {
                    list.addAll(menuBean.getFloors());
                    handler.sendEmptyMessage(0);
                }
            }
        });
    }

    private void initView() {
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(getActivity(),drawerLayout,toolbar,R.string.app_name,R.string.app_name);
        toggle.syncState();
        drawerLayout.addDrawerListener(toggle);
  }
}
